package 寒假刷题;

public class 图片平滑器 {
    int m;
    int n;
    public int[][] imageSmoother(int[][] img) {
        m = img.length;
        n = img[0].length;
        int[][] ints = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                ints[i][j] = method(img,i,j);
            }
        }
        return ints;
    }

    private int method(int[][] img, int i, int j) {
        int[][] dirs = {{-1,-1},
                       {-1,0},
                       {-1,1},
                       {0,-1},
                       {0,1},
                       {1,-1},
                       {1,0},
                       {1,1}};
        int count = 1,res = img[i][j];
        for (int[] dir : dirs) {
            int x = i + dir[0];
            int y = j + dir[1];
            if (x<0 || x>=m || y<0 || y>=n)
                continue;
            res+=img[x][y];
            count++;
        }
        return res/=count;
    }
}
